package com.zz.study.sharding.task;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.zz.study.sharding.util.SqlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import static com.zz.study.sharding.constants.Constants.CompressHisTrackDataTableNamePrefix;
import static com.zz.study.sharding.constants.Constants.HisTrackDataTableNamePrefix;

@Component
@Slf4j
@EnableScheduling
public class TableCreateTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Value("${bydata.feip}")
    private String feip;

    @Value("${bydata.database}")
    private String databaseName;


    @Value("${bydata.userName}")
    private String userName;

    @Value("${bydata.password}")
    private String password;



    @Scheduled(cron = "0 1/1 * * * ?")
    //@Scheduled(cron = "0/30 * * * * ?")
    public synchronized boolean scheduleCreateTableTask() throws SQLException {
        String tableName ="";
        String tableName2 ="";
        String nextMonthStartStr = "";
        String nextMonthEndStr = "";
        Date now = new Date();
       Date monthEndDate =  DateUtil.endOfMonth(now);
       long betweenDay =  DateUtil.between(now,monthEndDate, DateUnit.DAY);
       if(betweenDay<10){
            Date nextMonth = DateUtil.offsetDay(monthEndDate,5);
           tableName = com.zz.study.sharding.util.DateUtil.getShardTableName(CompressHisTrackDataTableNamePrefix,nextMonth);
           tableName2 = com.zz.study.sharding.util.DateUtil.getShardTableName(HisTrackDataTableNamePrefix,nextMonth);
          Date nextMonthStartDate = DateUtil.beginOfMonth(nextMonth);
           Date nextnextMonthDate = DateUtil.offsetMonth(nextMonthStartDate,1);
           nextMonthStartStr = DateUtil.format(nextMonthStartDate,"yyyy-MM-dd");
           nextMonthEndStr = DateUtil.format(nextnextMonthDate,"yyyy-MM-dd");


       }
        Statement stmt=null;
        boolean result = false;
        java.sql.Connection connection = null;
        try {


                String jdbcUrl = "jdbc:mysql://"+feip+":9030/"+databaseName+"?useUnicode=true&characterEncoding=utf8&autoReconnectForPools=true&useSSL=false";
                SqlUtil.creataStarRocksTable(jdbcUrl,userName,password,tableName,nextMonthStartStr,nextMonthEndStr);
            SqlUtil.creataStarRocksTable(jdbcUrl,userName,password,tableName2,nextMonthStartStr,nextMonthEndStr);
                result = true;

        } catch (Exception e) {
            e.printStackTrace ();
        }finally {
            if(connection !=null) {
                connection.close();
            }
        }
        return result;
    }
}
